<?php

/*
 *
 */

session_start();

include 'seesmic/date.php'; // seesmic date functions

include('../../../seesmicwsdbinfo-live.php');

// connect to the database
$wslink = mysql_connect($wsdbhost, $wsdbuser, $wsdbpass);
if (!$wslink) {
	die('Problem connecting to database server : ' . mysql_error());
}

// make $dbname the current db
$wsdb_selected = mysql_select_db($wsdbname, $wslink);
if (!$wsdb_selected) {
	die ('Problem connecting to database : ' . mysql_error());
}

// get any passed through variables
if (!$_GET['timeline']) {
	$timeline = "public";
} else {
	$timeline = $_GET['timeline'];
}

if (!$_GET['page']) {
	$page = 1;
	$offset = 0;
} else {
	$page = $_GET['page'];
	$offset = ($page - 1) * $_SESSION['squantity'];
}

$video_id = $_GET['video_id'];
$username = $_GET['username'];

$prev = $page - 1;
$next = $page + 1;

echo "<div id='nav'>";

// display current position
echo "Page ".$page;

switch ($timeline) {

	case "public" :

		// display previous button if appropriate
		if ($prev > 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:getTimeline('public',".$prev.",'yes');\">&lt;&nbsp;prev</a>";
		} elseif ($prev == 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:scrollPublicTimeline('yes');\">&lt;&nbsp;prev</a>";
		} else {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		// display next button
		echo "&nbsp;&nbsp;<a href=\"javascript:stopScroll();getTimeline('public',".$next.",'yes');\">next&nbsp;&gt;</a>";

		// set the appropriate api call url
//		$seesmicLatestVideosUrl = "http://v.seesmic.com/videos.json?sid=".$_SESSION['seesmicSid']."&offset=".$offset."&pagesize=".$_SESSION['squantity'];
		$seesmicLatestVideosUrl = "http://v.seesmic.com/videos.json?offset=".$offset."&pagesize=".$_SESSION['squantity'];

		break;

	case "friends" :

		// display previous button if appropriate
		if ($prev > 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:getTimeline('friends',".$prev.",'yes');\">&lt;&nbsp;prev</a>";
		} elseif ($prev == 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:scrollFriendsTimeline('yes');\">&lt;&nbsp;prev</a>";
		} else {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		// display next button
		echo "&nbsp;&nbsp;<a href=\"javascript:stopScroll();getTimeline('friends',".$next.",'yes');\">next&nbsp;&gt;</a>";

		// set the appropriate api call url
//		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$_SESSION['seesmicName']."/follows/videos.json?sid=".$_SESSION['seesmicSid']."&offset=".$offset."&pagesize=".$_SESSION['squantity'];
		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$_SESSION['seesmicName']."/follows/videos.json?offset=".$offset."&pagesize=".$_SESSION['squantity'];
		break;

	case "inbox" :

		// display previous button if appropriate
		if ($prev > 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:getTimeline('inbox',".$prev.",'yes');\">&lt;&nbsp;prev</a>";
		} elseif ($prev == 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:scrollInboxTimeline('yes');\">&lt;&nbsp;prev</a>";
		} else {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		// display next button
		echo "&nbsp;&nbsp;<a href=\"javascript:stopScroll();getTimeline('inbox',".$next.",'yes');\">next&nbsp;&gt;</a>";

		// set the appropriate api call url
		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$_SESSION['seesmicName']."/replies.json?sid=".$_SESSION['seesmicSid']."&offset=".$offset."&pagesize=".$_SESSION['squantity'];
		break;

	case "sent" :

		// display previous button if appropriate
		if ($prev > 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:getTimeline('sent',".$prev.",'yes');\">&lt;&nbsp;prev</a>";
		} elseif ($prev == 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:scrollSentTimeline('yes');\">&lt;&nbsp;prev</a>";
		} else {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		// display next button
		echo "&nbsp;&nbsp;<a href=\"javascript:stopScroll();getTimeline('sent',".$next.",'yes');\">next&nbsp;&gt;</a>";

		// set the appropriate api call url
		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$_SESSION['seesmicName']."/videos.json?sid=".$_SESSION['seesmicSid']."&offset=".$offset."&pagesize=".$_SESSION['squantity'];
//		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$_SESSION['seesmicName']."/videos.json?offset=".$offset."&pagesize=".$_SESSION['squantity'];
		break;

	case "thread" :

		// display previous button if appropriate
		if ($prev >= 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:getThreadTimeline('".$video_id."',".$prev.",'yes');\">&lt;&nbsp;prev</a>";
		} else {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		// display next button
		echo "&nbsp;&nbsp;<a href=\"javascript:getThreadTimeline('".$video_id."',".$next.",'yes');\">next&nbsp;&gt;</a>";

		// set the appropriate api call url
//		$seesmicLatestVideosUrl = "http://v.seesmic.com/threads/".$video_id.".json?sid=".$_SESSION['seesmicSid']."&offset=".$offset."&pagesize=".$_SESSION['squantity'];
		$seesmicLatestVideosUrl = "http://v.seesmic.com/threads/".$video_id.".json?offset=".$offset."&pagesize=".$_SESSION['squantity'];
		break;

	case "user" :

		// display previous button if appropriate
		if ($prev >= 1) {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;<a href=\"javascript:getUserTimeline('".$username."',".$prev.",'yes');\">&lt;&nbsp;prev</a>";
		} else {
			echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		}

		// display next button
		echo "&nbsp;&nbsp;<a href=\"javascript:getUserTimeline('".$username."',".$next.",'yes');\">next&nbsp;&gt;</a>";

		// set the appropriate api call url
//		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$username."/videos.json?sid=".$_SESSION['seesmicSid']."&offset=".$offset."&pagesize=".$_SESSION['squantity'];
		$seesmicLatestVideosUrl = "http://v.seesmic.com/users/".$username."/videos.json?offset=".$offset."&pagesize=".$_SESSION['squantity'];
		break;
}

echo "</div>";

$seesmic = "\n<div class=\"scroll\">\n";
$seesmic .= "\t<table id=\"sees_table\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";

$cs = curl_init($seesmicLatestVideosUrl);
curl_setopt($cs, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($cs, CURLOPT_USERAGENT, "seesmicsidebar"); //give your script an identity, so they can see you
curl_setopt($cs, CURLOPT_HEADER, 0);
curl_setopt($cs, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cs, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($cs, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($cs, CURLOPT_TIMEOUT, 55);
$result = curl_exec($cs);
$resultArray = curl_getinfo($cs); // this can be useful to see http headers in the reply using print_r($resultArray)

if (curl_errno($cs)) {

	// there is a curl error, display error and exit
	$sid = curl_error($cs);
	curl_close($cs);

	$seesmic = "<p><span style=\"color:white;\">";
	$seesmic .= $seesmicLatestVideosUrl;
	$seesmic .= "</span></p>";
	$seesmic .= date('d-m-Y H:i:s');
	$seesmic .= " curl_error sid: ";
	$seesmic .= $sid;
	$seesmic .= "\n";
	$seesmic .= date('d-m-Y H:i:s');
	$seesmic .= " curl error, exiting\n";
	echo $seesmic;
	exit;
} else {

	// there is a response
	$response = json_decode($result, true);
	if (!is_array($response)) {

		// something wrong with the response, display error and exit
		$sid = curl_error($cs);
		curl_close($cs);

		$seesmic .= "<p><span style=\"color:white;font-size: 75%;font-family: arial, 'lucida console', sans-serif;\"><a href=\"logout.php\">Either seesmic is down, or more likely the seesmic API has logged you out as it does after a certain length of time.<br /><br />To view the inbox or sent timelines you'll need to click anywhere on this text to log out. Then log back in again - your settings have been saved!</a></span></p>";
/*
	$seesmic .= "<p><span style=\"color:white;\">";
	$seesmic .= $seesmicLatestVideosUrl;
	$seesmic .= "</span></p>";
	$seesmic .= date('d-m-Y H:i:s');
	$seesmic .= " curl_error sid: ";
	$seesmic .= $sid;
	$seesmic .= "\n";
	$seesmic .= date('d-m-Y H:i:s');
	$seesmic .= " curl error, exiting\n";
*/
		echo $seesmic;
		exit;
	} else {

		// response ok so display results
		$resultArray = curl_getinfo($cs); // this can be useful to see http headers in the reply using print_r($resultArray)

		/* hide for live
		echo "<span style=\"color:white\">";
		echo $seesmicLatestVideosUrl." ";
		echo "<pre>";
		print_r($resultArray);
		echo "</pre><br /><pre>";
		print_r($response);
		echo "</pre></span>";
		*/

		foreach ($response as $array) {

			foreach ($array as $key => $value) {
				switch ($key) {
					case "url_thumbnail":
						$url_thumbnail = $value;
						if (substr($url_thumbnail,0,23) == "http://fms3.seesmic.com") {
							$ending = substr($url_thumbnail,23,strlen($url_thumbnail));
							$url_thumbnail = "http://t.seesmic.com".$ending;
						}
						break;
					case "url_player":
						$url_player = $value;
						break;
					case "title":
						$title = $value;
						break;
					case "username":
						$username = $value;
						break;
					case "language":
						$language = $value;
						break;
					case "created_at":
						$created_at = parse_datetime($value);
						break;
					case "thread_id":
						$thread_id = $value;
						break;
					case "reply_to":
						$reply_to = $value;
						break;
					case "url_flv":
						$url_flv = $value;
						if (substr($url_flv,0,23) == "http://fms3.seesmic.com") {
							$ending = substr($url_flv,23,strlen($url_flv));
							$url_flv = "http://v.seesmic.com".$ending;
						}
						break;
					case "video_id":
						$video_id = $value;
						break;
					case "thread_post_count":
						$thread_post_count = $value - 1;
						break;
					case "reply_to_username":
						$reply_to_username = $value;
						break;
				} // end switch ($key)
			} // end foreach ($array as $key => $value)

			// only display language selection on public timeline and check Qik

			$displayThis = true;

			if ((substr($title,0,5) == "[qik]") && ($_SESSION['qikStatus'] == "off"))
				$displayThis = false;

			if (
			(
			(($timeline == "public") && ($_SESSION['lan'] == $language))
			||
			(($timeline == "public") && ($_SESSION['lan'] == "all"))
			||
			($timeline != "public")
			) && $displayThis
			) {
				$seesmic .= "\t<tr>\n";

				// display the thumbnail image with link to play video

				$thumbnail_cache_uri = "../images/thumbnails/".$video_id.".jpg";

				$thumbnail_cache_url = "images/thumbnails/".$video_id.".jpg";

				if (file_exists($thumbnail_cache_uri)) {
					$thumbnail_cache_url = "images/thumbnails/".$video_id.".jpg";
				} else {
					$thumbnail_cache_url = $url_thumbnail;
				}

				$seesmic .= "\t<td align=\"middle\" width=\"82\" valign=\"top\" style=\"color:white;font-size: 75%;font-family: arial, 'lucida console', sans-serif;\">\n";
				$seesmic .="<a href=\"javascript:loadFlv('";
				$seesmic .= $url_flv;
				$seesmic .= "','";
				$seesmic .= $video_id;
				$seesmic .= "','";
				$seesmic .= addslashes($title);
				$seesmic .= "','";
				$seesmic .= $username;
				$seesmic .= "','";
				$seesmic .= $_SESSION['pwidth'];
				$seesmic .= "','";
				$seesmic .= $_SESSION['pheight'];
				$seesmic .= "','";
				$seesmic .= $_SESSION['sheight'];
				$seesmic .= "');\">";
				$seesmic .= "<img class=\"seesmic_img\" src=\"".$thumbnail_cache_url."\" width=\"80\" height=\"60\" style=\"border: 0px;\" alt=\"play video\" /></a>\n";

				$seesmic .= "\t<br />";

				// display number of replies (post count minus one)
				if ($thread_post_count >= 2) {
					$seesmic .= "\t<a href=\"javascript:stopScroll();getThreadTimeline('".$video_id."',1,'yes');\">".$thread_post_count." replies&nbsp;&gt;&gt;</a>\n";
				} elseif ($thread_post_count == 1) {
					$seesmic .= "\t<a href=\"javascript:stopScroll();getThreadTimeline('".$video_id."',1,'yes');\">1 reply&nbsp;&gt;&gt;</a>\n";
				} else {
					$seesmic .= "\t0 replies\n";
				}
				$seesmic .= "\t</td>\n";

				// display title of video with link to play if clicked
				$seesmic .= "\t<td valign=\"top\" valign=\"top\"><span style=\"color:white;font-size: 75%;font-family: arial, 'lucida console', sans-serif;\">";
				$seesmic .="<a href=\"javascript:loadFlv('";
				$seesmic .= $url_flv;
				$seesmic .= "','";
				$seesmic .= $video_id;
				$seesmic .= "','";
				$seesmic .= addslashes($title);
				$seesmic .= "','";
				$seesmic .= $username;
				$seesmic .= "','";
				$seesmic .= $_SESSION['pwidth'];
				$seesmic .= "','";
				$seesmic .= $_SESSION['pheight'];
				$seesmic .= "','";
				$seesmic .= $_SESSION['sheight'];
				$seesmic .= "');\">";
				$seesmic .= $title."</a></span>";

				// display time video posted
				$seesmic .= "\t<br /><span style=\"color:yellow;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><small>".$created_at."</small></span>\n";

				$wslinkstmt = "SELECT url FROM links WHERE video_id = '".$video_id."'";

				$wslinkres = mysql_query($wslinkstmt,$wslink);
				if (!$wslinkres) {
					die ('Problem with query : ' . mysql_error());
				}

				$wsrow = mysql_fetch_row($wslinkres);

				if ($wsrow) {
					$url = $wsrow[0];
					// display link
					$seesmic .= "<small><span style=\"color:green;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><a href=\"".$url."\" target=\"_blank\">link</a></span></small>\n";
				} else {
					// if is logged in user's video is showing then display add link
					if ($_SESSION['seesmicName'] == $username) {
						$seesmic .= "\t<span style=\"color:green;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><small><a href=\"javascript:getAddLink('".$video_id."','".$url_thumbnail."','".addslashes($title)."');\">add link</a></small></span>\n";
					}
				}

				$seesmic .= "\t<br />";

				// display profile icon
				$seesmic .= "\t<a href=\"javascript:getUserProfile('".$username."');\"><img src=\"images/profile.png\" border=\"0\" /></a>";

				// display username
				$seesmic .= "\t<div id='username'><a href=\"javascript:stopScroll();getUserTimeline('".$username."',1,'yes');\">".$username."</a></div>";

				// display language
				$seesmic .= "<span style=\"color:pink;font-size: 75%;font-family: arial, 'lucida console', sans-serif\"><small>&nbsp;(".$language.")</small></span>\n";

				// display download flv link
				$seesmic .= "<small><a href=\"".$url_flv."\" style=\"color:red;font-size: 75%;font-family: arial, 'lucida console', sans-serif\">flv</a></small>&nbsp;";

				// display link to play in seesmic
				$seesmic .= "<div id='seesmic_link'><a href=\"http://www.seesmic.com/Seesmic.html#/video/".$video_id."/watch\" target=\"_new\">seesmic</a></div>\n";

				$seesmic .= "\t</td></tr>\n";
			} // end language selection if
		} // end foreach ($response as $array)
	} // end if (!is_array($response))
} // end if (curl_errno($cs))
curl_close($cs);
mysql_close($wslink);
$seesmic .="</table></div>\n";
echo $seesmic;
?>